#!/bin/bash

set -ex

PYTHON="python3"

function run_payload {
    local payload="$1"
    sudo "$PYTHON" <<EOF
from __future__ import print_function
from ceph_volume_client import CephFSVolumeClient, VolumePath
from sys import version_info as sys_version_info
from rados import OSError as rados_OSError
import logging
log = logging.getLogger("ceph_volume_client")
log.addHandler(logging.StreamHandler())
log.setLevel(logging.DEBUG)
vc = CephFSVolumeClient("manila", "/etc/ceph/ceph.conf", "ceph")
vc.connect()
${payload}
vc.disconnect()
EOF
}

function import_key {
  local client="$1"
  if [ -n "$2" ]; then
    local keyring="$2"
  else
    local keyring="/etc/ceph/ceph.client.${client}.keyring"
  fi
  local T=$(mktemp)
  tee "$T" >&2
  sudo touch -- "$keyring"
  sudo ceph-authtool "$keyring" --import-keyring "$T"
  rm -f -- "$T"
}

function conf_keys {
  local client="$1"
  ls /etc/ceph >&2
  ceph auth get-or-create "client.manila" mds 'allow *' osd 'allow rw' mon 'allow *' | import_key "$client" /etc/ceph/ceph.keyring
}

function create_data_isolated {
  local PAYLOAD='
vp = VolumePath(None, "vol_isolated")
vc.create_volume(vp, (1<<33), data_isolated=True)
auth_result = vc.authorize(vp, "vol_data_isolated", tenant_id="test")
print("[client.vol_data_isolated]\n\tkey = ", auth_result["auth_key"])
'

  run_payload "$PAYLOAD" | import_key "vol_data_isolated"
}

function create_default {
  local PAYLOAD='
vp = VolumePath(None, "vol_default")
vc.create_volume(vp, (1<<33))
auth_result = vc.authorize(vp, "vol_default", tenant_id="test")
print("[client.vol_default]\n\tkey = ", auth_result["auth_key"])
'
  run_payload "$PAYLOAD" | import_key "vol_default"
}

function create {
  create_data_isolated
  create_default
}

function populate {
  pwd
  df -h .
  ls -l
  cp -a /usr/bin .
}

function verify_data_isolated {
  ceph fs subvolume getpath cephfs vol_isolated
  stat bin
  ls bin | tail
}

function verify_default {
  ceph fs subvolume getpath cephfs vol_default
  stat bin
  ls bin | tail
}

function verify {
  diff <(ceph fs subvolume ls cephfs | jq -cS 'sort_by(.name)' | tee /dev/stderr) <(printf '[{"name":"vol_isolated"},{"name":"vol_default"}]' | jq -cS 'sort_by(.name)')
  verify_data_isolated
  verify_default
}

function main {
  if [ "$1" = create ]; then
    conf_keys
    create
  elif [ "$1" = populate ]; then
    populate
  elif [ "$1" = verify ]; then
    # verify (sub)volumes still exist and are configured correctly
    verify
  else
    exit 1
  fi
}

main "$ACTION"
